package com.incar.chuxing.utils.ble.utils;

import android.util.Log;

public class Logger {
    public final static String LOGTAG = "BLE";

    public static final boolean DEBUG = true;

    // private static Lock lock = new ReentrantLock();
    // private static Object obj = new Object();

    public static void v(String msg) {
        Log.v(LOGTAG, getComString(null, msg));
    }

    public static void v(String title, String msg) {
        Log.v(LOGTAG, getComString(title, msg));
    }

    public static void e(String msg) {
        if (DEBUG) {
            String message = getComString(null, msg);
            Log.e(LOGTAG, message);
            writeLogFile(message);
        }
    }

    public static void e(String title, String msg) {
        if (DEBUG) {
            String message = getComString(title, msg);
            Log.e(LOGTAG, message);
            writeLogFile(message);
        }
    }

    public static String getComString(String title, String msg) {
        if (title == null) {
            return msg == null ? "" : msg;
        }
        return String.format("[%s]: %s", title, msg == null ? "" : msg);
    }

    public static void e(Throwable ex)// Exception ex)
    {

        e("", ex);

    }

    public static void e(String funName, Throwable ex) {
        if (DEBUG) {
            if (ex == null)
                return;

            String message = getComString(funName + "-!ERROR!", getStackTrace(ex));
            Log.e(LOGTAG, message);
            writeLogFile(message);
        }
    }

    public static void f(String msg) {
        String message = getComString(null, msg);
        Log.w(LOGTAG, message);
        // writeLogFile(message);
    }

    public static void f(String title, String msg) {
        String message = getComString(title, msg);
        Log.w(LOGTAG, message);
        // writeLogFile(message);
    }

    public static String getStackTrace(Throwable t)// Exception t)
    {
        return Log.getStackTraceString(t);
    }

    private static void writeLogFile(String msg) {
        // synchronized (obj)
        // {
        // try
        // {
        // File file = new
        // File(BaseApplication.getMyApplication().getFilesDir(),
        // "wsm.log");//new File(fileName);
        // FileWriter fw = null;
        // if(file.exists())
        // {
        // if (file.length() > 100000)
        // fw = new FileWriter(file, false);
        // else
        // fw = new FileWriter(file, true);
        // }
        // else
        // fw = new FileWriter(file, false);
        //
        // java.util.Date d=new java.util.Date();
        // java.text.SimpleDateFormat s= new
        // java.text.SimpleDateFormat("MM-dd HH:mm:ss");
        // String dateStr = s.format(d);
        //
        // fw.write(String.format("[%s] %s", dateStr,msg));
        // //fw.write(msg);//String.format("%s", msg));
        // fw.write(13);
        // fw.write(10);
        // fw.flush();
        // fw.close();
        // } catch (Throwable ex)
        // {
        // int i = 0;
        // //Helper.toastMessage(HiApp.getApp(), ex.getMessage());
        // }
        // }
    }
}
